<?
$url = "http://www.tthfanfic.com/latest.php";
	$fdat =  file ( $url );
while(list($key1,$val1)=each($fdat)){
	if(stristr($val1,"<div class='story")){
		$valA .="\n".$val1;
	}
}
$val1=$valA;
$valDat = explode("<div class='story",$val1);
while(list($key1,$val1)=each($valDat)){
	if(stristr($val1,"title titlecolors'>")){
		$collection=$collection+1;
		$trim1 = str_replace("title titlecolors'><a href='/Story-","",$val1);
		$t1 = stristr($trim1,"/");
		$dataA[$collection]['id'] = str_replace($t1,'',$trim1);
		$dataA[$collection]['title'] = trim(strip_tags(str_replace("'>",'',stristr($t1,"'>"))));
	}elseif(stristr($val1,"desc'>")){
		$dataA[$collection]['summary']=trim(strip_tags(str_replace("desc'>","",$val1)));
	}elseif(stristr($val1,"details'>")){
		//echo htmlspecialchars($val1)."<hr>";
		$vt1 = explode('/Author-',$val1);
		$vt2 = explode('</a>',$vt1[1]);
		$vt3 = explode("'>",$vt2[0]);
			$aidt =explode("/", $vt3[0]);
			$dataA[$collection]['author_id'] =$aidt[0];
			$dataA[$collection]['author'] = trim(str_replace('-:-','',strip_tags($vt3[1])));
		$vt3 = explode('Chapters&nbsp;[',$vt2[1]);
		$vt4 = explode("]",$vt3[1]);
		if($collection!=0){
			$dataA[$collection]['chapters'] = trim(str_replace("' onClick='return confirm(&quot;I am old enough to read this&quot;)","",str_replace("'>",'',$vt4[0])));
		}
		$vt1 = explode("Published&nbsp;[",$val1);
		$vt2 = explode("]",$vt1[1]);
			//echo $vt2[0]."<br>";
		if($collection!=0){
			$vt3 = explode("&nbsp;",$vt2[0]);
			$vt4 = $vt3[1]." ".$vt3[0]." 20".$vt3[2];
			$dataA[$collection]['published'] = date("Y-m-d", strtotime($vt4));				
		}
		$vt1 = explode("Updated&nbsp;[",$val1);
			$vt2 = explode("]",$vt1[1]);
			//echo $vt2[0]."<br>";
		if($collection!=0){
			$vt3 = explode("&nbsp;",$vt2[0]);
			$vt4 = $vt3[1]." ".$vt3[0]." 20".$vt3[2];
			$dataA[$collection]['updated'] = date("Y-m-d", strtotime($vt4));					
		}				
	}else{
		//echo htmlspecialchars($val1)."<hr>";
	}	
}
/*
if(1){
	if(stristr($val1,'/Story-')){
		//echo htmlspecialchars($val1)."<hr>";		
		$fdatT = explode('</table>',$val1);
		//echo htmlspecialchars($fdatT[1])."<hr>";	
		$fdat2 = explode('<div',$fdatT[1]);
		while(list($key,$val)=each($fdat2)){
			$val = str_replace('</div>','',$val);
//
				//$summary = true;
				if(stristr($val, " class='storytitle titlecolors'><a href='/Story-")){
					$valB = str_replace("class='storytitle titlecolors'>","",$val);
						$valA = explode('\'>',trim($valB));
						$chA = explode('&',$valA[0]);
						$idt = explode("/",str_replace("' onClick='return confirm(","",str_replace('<a href=\'/Story-','',$chA[0])));
						$dataA[$collection]['id'] = $idt[0];
						$dataA[$collection]['title'] = strip_tags($valA[1]);
					$summary = false;
				}
				if(stristr($val, " class='storydetails'>")){
					//echo htmlspecialchars(str_replace('</div>','',$val))."<hr>";	
					$vt1 = explode('/Author-',$val);
					$vt2 = explode('</a>',$vt1[1]);
					$vt3 = explode("'>",$vt2[0]);
					if($collection!=0){
						$aidt =explode("/", $vt3[0]);
						$dataA[$collection]['author_id'] =$aidt[0];
						$dataA[$collection]['author'] = trim(str_replace('-:-','',strip_tags($vt3[1])));
					}
					$vt3 = explode('Chapters&nbsp;[',$vt2[1]);
					$vt4 = explode("]",$vt3[1]);
					if($collection!=0){
						$dataA[$collection]['chapters'] = trim(str_replace("' onClick='return confirm(&quot;I am old enough to read this&quot;)","",str_replace("'>",'',$vt4[0])));
					}
					$vt1 = explode("Published&nbsp;[",$val);
					$vt2 = explode("]",$vt1[1]);
					//echo $vt2[0]."<br>";
					if($collection!=0){
						$vt3 = explode("&nbsp;",$vt2[0]);
						$vt4 = $vt3[1]." ".$vt3[0]." 20".$vt3[2];
						$dataA[$collection]['published'] = date("Y-m-d", strtotime($vt4));				
					}
					$vt1 = explode("Updated&nbsp;[",$val);
					$vt2 = explode("]",$vt1[1]);
					//echo $vt2[0]."<br>";
					if($collection!=0){
						$vt3 = explode("&nbsp;",$vt2[0]);
						$vt4 = $vt3[1]." ".$vt3[0]." 20".$vt3[2];
						$dataA[$collection]['updated'] = date("Y-m-d", strtotime($vt4));					
					}
				
				}

				if(stristr($val," class='storydesc'>")){
						$dataA[$collection]['summary'] = str_replace(" class='storydesc'>","",$val);
				}
//
		}
	}
	}
*/
/*	while(list($key1,$val1)=each($fdat)){
		echo htmlspecialchars($val1)."<hr>";		
	if(stristr($val1,'story.php')){
		$fdatT = explode('</table>',$val1);
		//echo htmlspecialchars($fdatT[1])."<hr>";	
		$fdat2 = explode('<div',$fdatT[1]);
		while(list($key,$val)=each($fdat2)){
			$val = str_replace('</div>','',$val);
//
				//$summary = true;
				if(stristr($val, " class='storytitle titlecolors'><a href='story.php?no=")){
					$valB = str_replace("class='storytitle titlecolors'>","",$val);
						$collection = $collection +1;
						$valA = explode('\'>',trim($valB));
						$chA = explode('&',$valA[0]);
						$dataA[$collection]['id'] = str_replace("' onClick='return confirm(","",str_replace('<a href=\'story.php?no=','',$chA[0]));
						$dataA[$collection]['title'] = strip_tags($valA[1]);
					$summary = false;
				}
				if(stristr($val, " class='storydetails'>")){
					//echo htmlspecialchars(str_replace('</div>','',$val))."<hr>";	
					$vt1 = explode('authors.php?no=',$val);
					$vt2 = explode('</a>',$vt1[1]);
					$vt3 = explode("'>",$vt2[0]);
					if($collection!=0){
						$dataA[$collection]['author_id'] = $vt3[0];
						$dataA[$collection]['author'] = trim(str_replace('-:-','',strip_tags($vt3[1])));
					}
					$vt3 = explode('chapter=',$vt2[1]);
					$vt4 = explode("'>",$vt3[1]);
					if($collection!=0){
						$dataA[$collection]['chapters'] = trim(str_replace("' onClick='return confirm(&quot;I am old enough to read this&quot;)","",str_replace("'>",'',$vt4[0])));
					}
					$vt1 = explode("Published&nbsp;[",$val);
					$vt2 = explode("]",$vt1[1]);
					//echo $vt2[0]."<br>";
					if($collection!=0){
						$vt3 = explode("&nbsp;",$vt2[0]);
						$vt4 = $vt3[1]." ".$vt3[0]." 20".$vt3[2];
						$dataA[$collection]['published'] = date("Y-m-d", strtotime($vt4));				
					}
					$vt1 = explode("Updated&nbsp;[",$val);
					$vt2 = explode("]",$vt1[1]);
					//echo $vt2[0]."<br>";
					if($collection!=0){
						$vt3 = explode("&nbsp;",$vt2[0]);
						$vt4 = $vt3[1]." ".$vt3[0]." 20".$vt3[2];
						$dataA[$collection]['updated'] = date("Y-m-d", strtotime($vt4));					
					}
				
				}

				if(stristr($val," class='storydesc'>")){
						$dataA[$collection]['summary'] = str_replace(" class='storydesc'>","",$val);
				}
//
		}
	}
	}
*/
//echo "<pre>";
//var_dump($dataA);
//exit();		
//old version

/*	while(list($key,$val)=each($fdat)){
		// Patch for evil link in summary 
		//$val = str_replace("<A HREF=\"http://www.tthfanfic.com/story.php?no=6114\">'Survival'</A>","'Survival'",$val);
	//echo htmlspecialchars($val)."<br>";		
		$summary = true;
		if(stristr($val, "story.php?")){
			$storydot = $storydot + 1;
			if($storydot==1){
				$collection = $collection +1;
				$valA = explode('\'>',trim($val));
				$chA = explode('&',$valA[0]);
				$dataA[$collection]['id'] = str_replace('<a href=\'story.php?no=','',$chA[0]);
				$dataA[$collection]['title'] = strip_tags($valA[1]);
			}else{
				$storydot=0;
			}
			$summary = false;
		}
		if(stristr($val,'<font class=\'small\'>')){
			$summary = false;
		}
		if(stristr($val, "authors.php")){
			$valA = explode('\'>',trim($val));
			$chA = explode('&',$valA[0]);
			if($collection!=0){
				$dataA[$collection]['author_id'] = str_replace('<a href=\'authors.php?no=','',$chA[0]);
				$dataA[$collection]['author'] = trim(str_replace('-:-','',strip_tags($valA[1])));
			}
			$summary = false;
		}
		if(stristr($val, "Chapters [")){
			$valA = explode(']',trim($val));
			$chA = explode('[',$valA[0]);
			$chB = explode('>',$chA[1]);
			//var_dump($chB);
			//echo "<br>\n";
			if($collection!=0){
				$dataA[$collection]['chapters'] = trim(str_replace('</a','',$chB[1]));
			}
			$summary = false;
		}
		if(stristr($val, "Published [")){
			$valA = explode(']',trim($val));
			$chA = explode('[',$valA[0]);
			if($collection!=0){
				$dataA[$collection]['published'] = $chA[1];				
			}
			$summary = false;
		}
		if(stristr($val, "Updated [")){
			$valA = explode(']',trim($val));
			$chA = explode('[',$valA[0]);
			if($collection!=0){
				$dataA[$collection]['updated'] = $chA[1];				
			}
			$summary = false;
		}
		if(stristr($val,'<font')){
			$summary = false;
		}
		if($summary){
			if($collection!=0){
				$dataA[$collection]['summaryT'] .= $val;
			}	
		}
	
	}
	$dataB = $dataA;
	while(list($key,$val)=each($dataB)){
		$temp = explode('<br>',$val['summaryT']);
		//echo htmlspecialchars($temp[0])."<hr>";
		
				$dataA[$key]['summary'] .= trim($temp[0]);
			

	}	
*/
echo "&nbsp;&nbsp;&nbsp;Scan Complete<br>";
$cnt = 0; $cnt_n = 0; $cnt_u = 0; $cnt_Q = 0;
//echo "<pre>";
//var_dump($dataA);
//exit();	

	while(list($key,$val)=each($dataA)){
		$sql = "INSERT INTO `fic_listing` ( `story_id` , `author_id` , `story_title` , `chapters` , `published` , `updated` , `status`,`summary`,`inserted` ) VALUES ('".$val['id']."', '".$val['author_id']."', '".str_replace("'","''",$val['title'])."', '".$val['chapters']."', '".$val['published']."', '".$val['updated']."', '?','".str_replace("'","''",$val['summary'])."', NOW())";
		$cnt = $cnt + 1;
		$rs1 = $db->Execute($sql);
		if($rs1===false){ 
			$err = $db->ErrorMsg();
			if(stristr($err,'Duplicate entry')){

				$sqlc = "SELECT * FROM fic_listing WHERE sitekey='T' and story_id='".$val['id']."'";
				$rsC = $db->Execute($sqlc);
				$upd = false;
				if($rsC->Fields('chapters')!=$val['chapters']){ $upd = true; }
				if($rsC->Fields('updated')!=$val['updated']){ $upd = true; }
				$insid = $rsC->Fields('id');
				$db->Execute("INSERT INTO fic_metaverse SET fic_id='".$insid ."',universe_id='13'");
				if($upd){
					$sqT = "chapters='".$val['chapters']."',updated='".$val['updated']."',inserted=NOW()";
					if($rsC->Fields('published')=='0000-00-00'){
						$sqT .= ", published='".$val['published']."'";						
					}
					if($rsC->Fields('author_id')=='0'){
						$sqT .= ", author_id='".$val['author_id']."'";						
					}
					if($rsC->Fields('summary')!=$val['summary']){
						$sqT .= ", summary='".str_replace("'","''",$val['summary'])."'";	
					}	
					if($rsC->Fields('story_title')!=$val['title']){
						$sqT .= ", story_title='".str_replace("'","''",$val['title'])."'";	
					}	
					if($rsC->Fields('status')=='?' ){
						$cnt_Q = $cnt_Q + 1;
						$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='T' and story_id='".$val['id']."'";
						$db->Execute($sqlU);
					}elseif( $rsC->Fields('status')=='N'){
						$cnt_u = $cnt_u + 1;
						$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='T' and story_id='".$val['id']."'";
						$db->Execute($sqlU);
					}elseif($rsC->Fields('status')=='S'){	
						$cnt_u = $cnt_u + 1;
						$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='T' and story_id='".$val['id']."'";					
						$db->Execute($sqlU);
					}elseif($rsC->Fields('status')!='I'){	
						$cnt_u = $cnt_u + 1;
						$sqlU = "UPDATE fic_listing SET ".$sqT.",status='U' WHERE sitekey='T' and story_id='".$val['id']."'";						
						$db->Execute($sqlU);
					}elseif($rsC->Fields('status')=='I'){	
						$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='T' and story_id='".$val['id']."'";						
						$db->Execute($sqlU);
					}
					//echo $sqlU."<br>";
				}
				//echo "<br>";
			}else{
				echo "<br><b>".$err."</b>".$sql."<br>";
			}
		}else{
			$insid = $db->Insert_ID();
			$db->Execute("INSERT INTO fic_metaverse SET fic_id='".$insid ."',universe_id='13'");
			$cnt_n = $cnt_n + 1;
			//if new story try to insert author		
			$sql2 = "INSERT INTO `fic_authors` ( `author_id` , `author_name` )VALUES ('".$val['author_id']."', '".str_replace("'","''",$val['author'])."')";
			$rs2 = $db->Execute($sql2);
		}
	}

echo '&nbsp;&nbsp;&nbsp;Update Completed<br>';
echo '&nbsp;&nbsp;&nbsp;'.$cnt.' total,'.$cnt_n.'('.$cnt_Q.') new,'.$cnt_u.' updated<br>';

$db->Execute("UPDATE fic_authors SET linkid=id WHERE linkid='0'");

?>
